﻿var cIsIE = document.all;
function fSetEditable(frameObj) {
    frameObj.document.designMode = "on";
    //    if (!cIsIE)
    //        f.contentWindow.document.designMode = "on";
}
window.editorCssText = "";
function getEditorStyle() {
    if (window.editorCssText == "") {
        var cssText = [];
        cssText.push("*{margin:0;padding:0;line-height:150%;}");
        cssText.push("body{line-height:1.5em;font-size:12px;margin:8px 8px 0 10px;background:#fff}");
        cssText.push("ul,ol{margin-left:36px}");
        cssText.push("h2{background:#D1F3FD;font-size:12px;padding:6px 0 2px 11px;font-weight:normal}");
        cssText.push(".text{margin:20px 10px 20px 22px;line-height:1.7em;}");
        cssText.push("BLOCKQUOTE{margin-left:36px;}");

        window.editorCssText = cssText.join("");
    }
    return window.editorCssText;
}
function fSetHtmlContent(frameObj, bodyContent) {
    var f = frameObj;
    var htmlContent = "<head><style type=\"text/css\">" + getEditorStyle() + "</style></head>";
    htmlContent = htmlContent + "<body>" + bodyContent + "</body>"; //<div></div>
    f.document.open("text/html", "replace");
    f.document.write(htmlContent);
    f.document.close();
}
function fSetEvent(frameObj) {
    var f = frameObj;
    if (cIsIE) {
        f.document.onkeydown = function () { fHideMenu(); }
        f.document.onkeypress = function () { fHideMenu(); }
        f.document.onselect = function () { fHideMenu(); }
        f.document.onclick = function () { fHideMenu(); }
        f.document.body.onpaste = function () {
            var data = window.clipboardData.getData("text");
            //data = data.replace(/\r\n\r\n/gi, "<br/>");
            data = data.replace(/\r\n/gi, "<br/>");
            //data = data.replace(/\s{1}/gi, "&nbsp;")
            var reg = new RegExp("\\s{2,}", "i");
            var results = reg.exec(data);
            var empty = "";
            var maxValue = 1;
            while (results) {
                maxValue = results.lastIndex - results.index;
                for (var i = 1; i < maxValue; i++) {
                    empty += "&nbsp;";
                }
                empty += " ";
                data = data.replace(/\s{2,}/i, empty);
                empty = "";
                results = reg.exec(data);
            }
            window.frames["HtmlEditor"].document.selection.createRange().pasteHTML(data);
            //window.clipboardData.setData("text",);
            return false;
        };
    }
}

jDoc.addEventHandler(document.body, "click", function (e) {
    if (cIsIE) var el = event.srcElement;
    else var el = e.target;
    if (el.id == "imgFontface" || el.id == "imgFontsize" || el.id == "imgSymbol") {

    } else {
        fHideMenu();
    }
});

function InitialEditor(text) {
    try {
        if (text == undefined) { text = ""; }
        var frameObj = window.frames["HtmlEditor"];
        fSetEditable(frameObj);
        fSetHtmlContent(frameObj, text);
        fSetEvent(frameObj);
    } catch (e) {
    }
}

function fDisplayElement(element, parentElement, displayValue) {
    fHideMenu();
    var element = jDoc(element);
    var parent = jDoc(parentElement);
    if (element == null || parent == null) return;
    element.style.display = "";
    var pos = $(parent).position();
    var diff = getPosDiff();
    if (cIsIE) {
        element.style.left = (pos.left - diff.x) + "px";
        element.style.top = (pos.top + diff.y) + "px";
    }
    else {
        element.style.left = (pos.left - diff.x) + "px";
        element.style.top = (pos.top + diff.y) + "px";
    }
    return true;
}
function getPosDiff() {
    var diff = {};
    if (cIsIE) {
        diff = { x: 30, y: -60 };
        if (parseInt($.browser.version, 10) > 6) {
            diff.y = 40;
        }
    } else {
        diff = { x: 33, y: 40 };
    }
    return diff;
}
function format(type, para) {
    var f = window.frames["HtmlEditor"];
    var sAlert = "";
    if (!cIsIE) {
        switch (type) {
            case "cut":
                sAlert = "你的浏览器安全设置不允许编辑器自动执行剪切操作,请使用键盘快捷键(Ctrl+X)来完成";
                break;
            case "copy":
                sAlert = "你的浏览器安全设置不允许编辑器自动执行拷贝操作,请使用键盘快捷键(Ctrl+C)来完成";
                break;
            case "paste":
                sAlert = "你的浏览器安全设置不允许编辑器自动执行粘贴操作,请使用键盘快捷键(Ctrl+V)来完成";
                break;
        }
    }
    if (sAlert != "") {
        alert(sAlert);
        return;
    }
    f.focus();
    if (!para)
        if (cIsIE)
            f.document.execCommand(type);
        else
            f.document.execCommand(type, false, false);
    else {
        if (para == "楷体") para = "楷体_GB2312";
        f.document.execCommand(type, false, para);
    }
    f.focus();
}

function fCheckIfFontFace(obj) {
    if (obj.parentNode) {
        if (obj.parentNode.id == "he_Fontface") return true;
        else return fCheckIfFontFace(obj.parentNode);
    } else {
        return false;
    }
}

function fSetBorderMouseOver2(obj) {
    obj.className = "mover2"
}
function fSetBorderMouseOut2(obj) {
    obj.className = "img2"
}
function fSetBorderMouseDown2(obj) {
    obj.className = "mdown2"
}

function fSetBorderMouseOver(obj) {
    if (obj.className != "mdown") obj.className = "mover";
}
function fSetBorderMouseOut(obj) {
    if (obj.className != "") obj.className = "";
}
function fSetBorderMouseDown(obj) {
    if (obj.className != "mdown") obj.className = "mdown";
    else obj.className = "";
}

function insertSymbol(obj) {
    var f = window.frames["HtmlEditor"];
    f.focus();
    if (cIsIE) {
        f.document.selection.createRange().pasteHTML(obj);
    } else {
        format("insertHTML", obj);
    }
}
function fHideMenu() {
    var fontface = jDoc("he_Fontface");
    var fontsize = jDoc("he_FontSize");
    var symbol = jDoc("he_Symbol");
    fontface.style.display = "none";
    fontsize.style.display = "none";
    symbol.style.display = "none";
}
function browse() {
    var oWin = window.open("", null, "resizable=yes,scrollbars=yes", false);
    oWin.document.write("<head><style type=\"text/css\">" + getEditorStyle() + "</style></head>");
    oWin.document.write("<body>" + GetHTMLValue() + "</body>");
    oWin.document.close();
    oWin.focus();
}

// 提交表单
function GetHTMLValue() {
    var html = window.frames["HtmlEditor"].document.getElementsByTagName("BODY")[0].innerHTML;
    if ((html.toLowerCase() == "<p>&nbsp;</p>") || (html.toLowerCase() == "<p></p>")) html = "";
    return filterHtmlTags(html);
}
// 获取iframe文本值
function GetTextValue() {
    window.frames["HtmlEditor"].document.getElementsByTagName("BODY")[0].innerHTML = GetHTMLValue();

    if (cIsIE) return FilterSpecialCharacter(window.frames["HtmlEditor"].document.getElementsByTagName("BODY")[0].innerText);
    else return FilterSpecialCharacter(window.frames["HtmlEditor"].document.getElementsByTagName("BODY")[0].textContent);
}

function FilterSpecialCharacter(html) {
    html = html.replace(/<!.*>/gi, "");
    html = html.replace(/[\[\]\{\}\(\)［］｛｝（）【】<>]+/gi, "");
    return html;
}

/*word filter*/
function filterHtmlTags(_sContent) {
    _sContent = _sContent.replace(/<\\?\?xml[^>]*>/gi, "");
    _sContent = _sContent.replace(/<\/?\w+:[^>]*>/gi, "");
    _sContent = _sContent.replace(/<!--[\s\S]*?-->/gi, "");
    var _oResult = stripData(_sContent, []);
    this.bHarm = _oResult.isHarm;
    _sContent = _oResult.content;
    return _sContent;
}

function stripData(content, noStripTags) {
    var badContent = ["head", "script", "style", "object", "applet", "noscript", "frameset", "noframes"];
    var badTag = ["form", "meta", "body", "html", "label", "select", "optgroup", "option", "textarea", "title", "script", "xmp", "applet", "embed", "head", "frameset", "iframe", "noframes", "noscript", "object", "style", "marquee", "a", "table", "tr", "td", "tbody", "input", "base", "basefont", "isindex", "link", "frame", "param", "xml", "xss", "st1:chsdate"];
    var badAction = ["on[a-z]+?", "disabled", "id", "name", "class"];
    var badCss = ["position", "javascript", "vbscript", "actionscript", "xmp", "activex"];
    var isHarm = false;
    var regStr;
    var reg = new RegExp("(&#)|(&%)", "ig");
    if (reg.test(content)) {
        content = content.replace(reg, "&");
    }
    delete reg;
    reg = new RegExp("(\t)", "ig");
    if (reg.test(content)) {
        content = content.replace(reg, " ");
    }
    delete reg;
    regStr = "(<[^<>]*)(\r|\n)([^>]*>)";
    reg = new RegExp(regStr, "ig");
    while (reg.test(content)) {
        content = content.replace(reg, "$1 $3");
        delete reg;
        reg = new RegExp(regStr, "ig");
    }
    delete reg;
    regStr = "(<[^<>]*)(\\\\|/\\*.*\\*/)([^>]*>)";
    reg = new RegExp(regStr, "ig");
    while (reg.test(content)) {
        content = content.replace(reg, "$1$3");
        delete reg;
        reg = new RegExp(regStr, "ig");
    }
    delete reg;
    for (var i = 0; i < badContent.length; i++) {
        if (findNoStripTag(badContent[i], noStripTags))
            continue;
        regStr = "<\\s*" + badContent[i] + "[^>]*>[\\s\\S]*?<\\s*/\\s*" + badContent[i] + "[^>]*>";
        reg = new RegExp(regStr, "ig");
        while (reg.test(content)) {
            isHarm = true;
            content = content.replace(reg, "");
            delete reg;
            reg = new RegExp(regStr, "ig");
        }
        delete reg;
    }
    for (var i = 0; i < badTag.length; i++) {
        if (findNoStripTag(badTag[i], noStripTags))
            continue;
        regStr = "<\\s*[/\?]?\\s*" + badTag[i] + "[^>]*>";
        reg = new RegExp(regStr, "ig");
        while (reg.test(content)) {
            isHarm = true;
            content = content.replace(reg, "");
            delete reg;
            reg = new RegExp(regStr, "ig");
        }
        delete reg;
    }
    for (var i = 0; i < badAction.length; i++) {
        if (findNoStripTag(badAction[i], noStripTags))
            continue;
        regStr = "(<[^<>]*[\\s'\"])" + badAction[i] + "\\s*=\\s*['\"]?[^\\s'\">]*[\\s'\"]?([^>]*>)";
        reg = new RegExp(regStr, "ig");
        while (reg.test(content)) {
            if (badAction[i] != "allowScriptAccess" && badAction[i] != "allowNetworking")
                isHarm = true;
            content = content.replace(reg, "$1$2"); //
            delete reg;
            reg = new RegExp(regStr, "ig");
        }
        delete reg;
    }
    for (var i = 0; i < badCss.length; i++) {
        if (findNoStripTag(badCss[i], noStripTags))
            continue;
        regStr = "(<[^<>]*)" + badCss[i] + "\\s*:\\s*[^\\s;\">]*([^>]*>)";
        reg = new RegExp(regStr, "ig");
        while (reg.test(content)) {
            isHarm = true;
            content = content.replace(reg, "$1$2");
            delete reg;
            reg = new RegExp(regStr, "ig");
        }
        delete reg;
    }
    regStr = "(<[^<>]*)expression\\s*\\([^\\)]*\\)([^>]*>)";
    reg = new RegExp(regStr, "ig");
    while (reg.test(content)) {
        isHarm = true;
        content = content.replace(reg, "$1$2");
        delete reg;
        reg = new RegExp(regStr, "ig");
    }
    delete reg;
    regStr = "(<[^<>]*)url\\s*\\([^\\)]*\\.(js|do)\\s*\\)([^>]*>)";
    reg = new RegExp(regStr, "ig");
    while (reg.test(content)) {
        isHarm = true;
        content = content.replace(reg, "$1$3");
        delete reg;
        reg = new RegExp(regStr, "ig");
    }
    delete reg;
    regStr = "(<[^<>]*[\\s'\"])src\\s*=\\s*['\"]?.*?\\.(js|do)(>)";
    reg = new RegExp(regStr, "ig");
    while (reg.test(content)) {
        isHarm = true;
        content = content.replace(reg, "$1$3");
        delete reg;
        reg = new RegExp(regStr, "ig");
    }
    delete reg;
    regStr = "(<[^<>]*[\\s'\"])src\\s*=\\s*['\"]?.*?\\.(js|do)[\\s'\"]([^>]*>)";
    reg = new RegExp(regStr, "ig");
    while (reg.test(content)) {
        isHarm = true;
        content = content.replace(reg, "$1$3");
        delete reg;
        reg = new RegExp(regStr, "ig");
    }
    delete reg;
    reg = new RegExp("(http|https)://", "ig");
    content = content.replace(reg, "");
    delete reg;
    var retobj = {};
    retobj.content = content;
    retobj.isHarm = isHarm;
    return retobj;
}
function findNoStripTag(tag, tags) {
    if (tags == null || tags == "")
        return false;
    for (var i = 0; i < tags.length; i++) {
        if (tags[i] == tag)
            return true;
    }
    return false;
}